

<?php

class Students extends Controller {

    function Students() {
        parent::Controller();
        $this->load->model('mod_students');
    }

    //==================this is function for using loading pagination ======================//

    function index() {
        if($this->check_session()){
        $this->load->view('master');
        }  else {
            redirect('admin/');
            exit();
        }
    }

    //===============this is function for using loading pagination =====================//

    function manager() {
        if($this->check_session()){
        $query_student['value_student'] = $this->mod_students->select_view();
        $this->load->view('master', $query_student);
        $data['title'] = 'Students';
        }  else {
            redirect('admin/');
            exit();
        }
    }

    //===========================this is function for using adding new student======================//


    function add() {
        if($this->check_session()){
        $data['title'] = 'this is the add new page';
        $this->load->helper("mgpage");
        $this->form_validation->set_rules('txt_studentname', 'studentname', 'required|trim|min_length[2]|max_length[30]');
        //$this->form_validation->set_rules('text_dob', 'Date of birth', 'required|trim|max_length[30]');
        //$this->form_validation->set_rules('txt_phone', 'phone', 'required|trim|max_length[30]');
        $this->form_validation->set_rules('studentsex', 'Sex', 'required|trim|max_length[30]');
        //$this->form_validation->set_rules('txt_mail', 'Email', 'required|trim|valid_email|max_length[30]');
        //  $this->form_validation->set_rules('txtarea', 'Detail', 'required|trim|max_length[30]');*/
        if ($this->input->post('btn_submit')) {
            if ($this->form_validation->run() == false) {
                $this->load->view('master', $data);
            } else {
                $this->mod_students->add();
                $this->session->set_userdata('ms', '<br/><dl id="system-message"><dd class="message message"><ul><li>Add Successful!</li></ul></dd></dl>');
                redirect('students/manager');
            }
        } else {
            $this->load->view('students/add');
        }
        }else{
            redirect('admin/');
            exit();
        }
    }

    //===========================this is function for using  view detail=========================//

    function detail() {
        if($this->check_session()){
        $getview['getvalue'] = $this->mod_students->viewdata();
        $this->load->view('master', $getview);
        }else{
            redirect('admin/');
            exit();
        }
    }

    //========================this is function for using check edit in student==================//

    function edit() {
        if($this->check_session()){
        if ($this->input->post('btn_update')) {//if submit form, it will be updated 
            $this->mod_students->edit();
            $this->session->set_userdata('editstudent_ms', '<br/><dl id="system-message"><dd class="message message"><ul><li>Updated Successful!</li></ul></dd></dl>');
            redirect('students/manager');
        } else {// if not submit form, it will select to view for edit
            $data['select'] = $this->mod_students->select_edit();
            $this->load->view('master', $data);
        }
        }  else {
            redirect('admin/');
            exit();
        }
    }

    //==========================this is function for using delete record of students==================//

    function delete() {
        if($this->check_session()){
        $this->mod_students->delete();
        $this->session->set_userdata('studentdel_ms', '<br/><dl id="system-message"><dd class="message message"><ul><li>Deleted Successful!</li></ul></dd></dl>');
        redirect('students/manager');
        }else{
            redirect('admin/');
            exit();
        }
    }

    //=====================this is function for search session========================//

    function search() {
        if($this->check_session()){
        if ($this->input->post('btnsubmit')) {
            $query_search['value_student'] = $this->mod_students->search();
            $this->load->view('master', $query_search);
        } else {
            echo 'No item search';
        }
        }else{
            redirect('admin/');
            exit();
        }
    }

    //====================this is the end of function search session================//
    
    
     //========this is function for using checking session=============
    
     public function check_session() {
        if ($this->session->userdata('use_username')) {
            return true;
        } else {
            return false;
        }
    }
}